package bieche.eric.beans;

import java.util.Iterator;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

/*
     <script type="text/javascript">
     //
     // * Set focus on the element of the given id.
     // * @param id The id of the element to set focus on.
     //
     function setFocus(id)
     {
         var element = document.getElementById(id);
         if (element && element.focus)
         {
             element.focus();
         }
     }

     //
     // * Set highlight on the elements of the given ids. It basically sets the classname of the elements
     // * to 'highlight'. This require at least a CSS style class '.highlight'.
     // * @param ids The ids of the elements to be highlighted, comma separated.
     //
     function setHighlight(ids)
     {
         var idsArray = ids.split(",");
         for (var i = 0; i < idsArray.length; i++)
         {
             var element = document.getElementById(idsArray[i]);
             if (element)
             {
                 element.className = 'highlight';
             }
         }
     }
     </script> 
     
     
     <script>
       setHighlight('${highlight}');
       setFocus('${focus}');
     </script>
 */


/**
 * This phase listener checks if there is a client ID with message and will set
 * the client ID as ${focus} in the request map. It will also gather all client IDs with message
 * and will set it as ${highlight} in the request map.
 * <p>
 * This phase listener should be configured in the faces-config.xml as follows:
 * 
 * <pre>
 * &lt;lifecycle&gt;
 *     &lt;phase-listener&gt;net.balusc.webapp.SetFocusListener&lt;/phase-listener&gt;
 * &lt;/lifecycle&gt;
 * </pre>
 * 
 * @author BalusC
 * @link http://balusc.blogspot.com/2007/12/set-focus-in-jsf.html
 */
public class SetFocusListener implements PhaseListener
{
   // Actions
   // ------------------------------------------------------------------------------------

   /**
    * @see javax.faces.event.PhaseListener#getPhaseId()
    */
   public PhaseId getPhaseId()
   {
      // Listen on render response phase.
      return PhaseId.RENDER_RESPONSE;
   }

   /**
    * @see javax.faces.event.PhaseListener#beforePhase(javax.faces.event.PhaseEvent)
    */
   public void beforePhase( PhaseEvent event )
   {
      // Init.
      FacesContext facesContext = event.getFacesContext();
      String focus = null;
      StringBuilder highlight = new StringBuilder();

      // Iterate over all client ID's with messages.
      Iterator< String > clientIdsWithMessages = facesContext.getClientIdsWithMessages();
      while ( clientIdsWithMessages.hasNext() )
      {
         String clientIdWithMessages = clientIdsWithMessages.next();
         if ( focus == null )
         {
            focus = clientIdWithMessages;
         }
         highlight.append( clientIdWithMessages );
         if ( clientIdsWithMessages.hasNext() )
         {
            highlight.append( "," );
         }
      }

      // Set ${focus} and ${highlight} in JSP.
      facesContext.getExternalContext().getRequestMap().put( "focus", focus );
      facesContext.getExternalContext().getRequestMap().put( "highlight", highlight.toString() );
   }

   /**
    * @see javax.faces.event.PhaseListener#afterPhase(javax.faces.event.PhaseEvent)
    */
   public void afterPhase( PhaseEvent event )
   {
      // Do nothing.
   }
}// End of Class